import sys
from socket import *

if len(sys.argv)!=2:
    quit()
    
annuaire = {"Standard"  : 6500,
            "Bidet"     : 6560,
            "Boimon"    : 6562,
            "Cottenceau": 6563,
            "Declerck"  : 6568,
            "Godon"     : 6570,
            "Hardouin"  : 6564,
            "Ferrier"   : 6574,
            "Lahaie"    : 6565} 


HOST,PORT='localhost',int(sys.argv[1])
if PORT < 1024:
    PORT = 2500

s=socket(AF_INET,SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(5)

try:
    while 1:
        connection,address=s.accept()        
        data = connection.recv(1024)
        print"received from ",address,":",data
        if data.upper().strip() == "ALL":
            ch = "";
            for i in annuaire.keys() :
                ch += i + " / ";
            
            connection.send(ch)
        else:
            if data.strip() in annuaire:
                connection.send(str(annuaire[data.strip()]))
            else:
                connection.send("Error")
        connection.close()
except KeyboardInterrupt:
    s.close()

